package com.example.myutil;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

public class HttpRequestUtil {
	
	private HttpRequestUtil(){}
	
	/**
	 * 是否是ajax请求
	 * @param request
	 * @return
	 */
	public static boolean isAjaxRequest(HttpServletRequest request){
		return "XMLHttpRequest".equals(request.getHeader("X-Requested-With"));
	}
	
	/**
	 * 访问项目的全路径， http://localhost:8080/sgsq_web/
	 * @return
	 */
	public static String getProjectRequestPath(HttpServletRequest request){
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ request.getContextPath() + "/";
		return basePath;
	}
	
	/**
	 * 下载最新apk的全路径地址 http://localhost:8080/sgsq_web/agrithings/file/downloadApp
	 * @param request
	 * @return
	 */
	public static String getNewestApkDownUrl(HttpServletRequest request){
		String projectPath = getProjectRequestPath(request);
		return projectPath + "agrithings/file/downloadApp";
	}
	
	/**
	 * 获取请求链接，用于与数据库配置的菜单链接比较
	 * @param request
	 * @return
	 */
	public static String getRequestURI(ServletRequest request){
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		String requestURI = httpRequest.getServletPath();
		return getRequestURI(requestURI);
	}
	
	/**
	 * 获取请求链接，用于与数据库配置的菜单链接比较
	 * @param url
	 * @return
	 */
	public static String getRequestURI(String url){
		String result = url;
		if(StringUtils.isNotBlank(url)){
			int pos = url.lastIndexOf(".do");//去掉.do
			if(pos != -1){
				url = url.substring(0, pos);
			}
			if(url.startsWith("/")){
				url = url.substring(1);//去掉url前的"/"
			}
			//去掉"?"后面的参数
			if(url.contains("?")){
				url = url.substring(0, url.indexOf("?"));
			}
			result = url;
		}
		return result;
	}
	
	/**
	 * 获取请求链接，用于与数据库配置的菜单链接比较,链接包含项目路径
	 * @param url
	 * @return
	 */
	public static String getRequestURI(HttpServletRequest request,String url){
		String projectName = request.getContextPath();
		String withouContextUrl = url.substring(url.indexOf(projectName)+projectName.length());
		return getRequestURI(withouContextUrl);
	}
	
	/**
	 * 退出登录的url
	 * @param request
	 * @param isAdmin
	 * @return
	 */
	public static String getLogoutUrl(HttpServletRequest request,boolean isAdmin){
		String contextPath = request.getContextPath()+"/";
//		String logoutUrl = contextPath+"agrithings/loginout";
		String logoutUrl = contextPath+"logout";
		if(isAdmin){
			logoutUrl += "?redirectUrl=/admin";
		}
		return logoutUrl;
	}
	
	/**
	 * 获取Server地址
	 * @param request
	 * @return
	 */
	public static String getServerHostAddr(HttpServletRequest request){
		String serverAddr  = "";
		try{
			String requestUrl = request.getRequestURL().toString();
			String context = request.getContextPath();
			serverAddr  = requestUrl.substring(0, requestUrl.indexOf(context));
		}
		catch(Exception e){
			
		}
		return serverAddr;
	}
	
	
	/**
	 * 根据请求获取请求服务的地址
	 * @param request
	 * @return
	 */
	public static final String getRequestHost(HttpServletRequest request){
		
		String requestUrl = request.getRequestURL().toString();
		String context = request.getContextPath();
		String serverAddr  = requestUrl.substring(0, requestUrl.indexOf(context));
		
		return serverAddr;
	}
}
